package com.sc.nft.task;

import com.sc.nft.dao.SandWithdrawRecordDao;
import com.sc.nft.dto.HnaWithdrawDto;
import com.sc.nft.entity.SandWithdrawRecord;
import com.sc.nft.enums.WalletChannelEnum;
import com.sc.nft.helper.LlPayHelper;
import com.sc.nft.service.UserHnaExtService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class QueryWithdrawTask {
  private final SandWithdrawRecordDao sandWithdrawRecordDao;
  private final LlPayHelper payHelper;
  private final UserHnaExtService userHnaExtService;

  @XxlJob("llApproval")
  public void llApproval() throws Exception {

    List<SandWithdrawRecord> sandWithdrawRecords =
        sandWithdrawRecordDao.queryWithApproval(WalletChannelEnum.LL, WalletChannelEnum.HNA);
    for (SandWithdrawRecord sandWithdrawRecord : sandWithdrawRecords) {
      if (WalletChannelEnum.LL.equals(sandWithdrawRecord.getChannel())) {
        payHelper.withdrawConfirm(sandWithdrawRecord, true, null);
      } else if (WalletChannelEnum.HNA.equals(sandWithdrawRecord.getChannel())) {
        HnaWithdrawDto dto = new HnaWithdrawDto();
        dto.setTranAmount(sandWithdrawRecord.getAmount().toString());
        dto.setFeeAmount(sandWithdrawRecord.getFeeAmount().toString());
        dto.setBindCardAgrNo(sandWithdrawRecord.getRelatedCardNo());
        dto.setMerUserIp("127.0.0.1");
        userHnaExtService.doWithdraw(sandWithdrawRecord, true, dto, "定时任务执行");
      }
    }
  }
}
